import { getCuesSubmitPage } from "../../../../../service/api/api-cues-submit";
import { selectCuesTopicManage } from "../../../../../service/api/api-cues-topic";

// pages/cues/topic/details/topicRenderDetail/index.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentUser: {},//当前用户
    access: {} as any,// 当前用户权限
    topicId: 0,//当前选题征集的id
    current: 1,//选题征集表格当前页
    pageSize: 20,//选题征集表格条数
    dataTopicDetails: {},//选题征集详情
    activeAssignOrganizations: false,//指派征集单位是否显示
    activeLinkageOrganizations: false,//协助媒体单位是否显示
    DetailDrawerVisible: false,//选题详细信息是否显示
    cuesSubmitPageList: [],//选题征集表格数据
    mode: 0,//选题征集上报线索
    recordId: 0,//我都选题征集抽屉右上角的申请记录
    recordCount: 0,//记录上报次数
    submitOrganizationCount: 0,//记录上报单位数
  },
  //获取选题征集表格
  async getCuesSubmitPage(params: { current: number, pageSize: number, topicId: number, mode: number }) {
    const response = await getCuesSubmitPage({ current: params.current, pageSize: params.pageSize, topicId: params.topicId, mode: params.mode });
    this.setData({
      cuesSubmitPageList: response.data.data,

    })
  },
  /**
 * 加载 选题计划详情
 * @param key
 */
  async loadCuesTopicDetails(id) {
    try {
      const response = await selectCuesTopicManage({ id: id });
      const { code, data } = response;
      if (code === 0) {
        this.setData({
          dataTopicDetails: data,
          recordCount: data.countSubmit ? data.countSubmit : 0,
          submitOrganizationCount: data.countSubmitOrganization ? data.countSubmitOrganization : 0,
        })
      }
    } catch (error) { }
  },
  //打开详细信息抽屉
  onOpenDetailDrawer() {
    this.setData({
      DetailDrawerVisible: true
    })
  },
  //开关指派征集单位
  handleAssignOrganizationsChange(e) {
    this.setData({
      activeAssignOrganizations: true,
    });
  },
  //开关协助媒体单位
  handleLinkageOrganizationsChange(e) {
    this.setData({
      activeLinkageOrganizations: true,
    });
  },
  //上报线索
  handleCreateSubmit() {
    wx.navigateTo({
      url: '/pages/cues/topic/create/createSubmit/index?topicId=' + this.data.topicId + '&mode=' + this.data.mode + '&topicName=' + this.data.dataTopicDetails.name + '&index=' + this.data.index
    })
  },
  //跳转到申请记录界面
  goToRecordDetail() {
    wx.navigateTo({
      url: '/pages/cues/topic/details/index?topicId=' + this.data.topicId + '&mode=' + this.data.mode + '&recordId=' + this.data.recordId
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: { topicId: any; mode: any, recordId: number, index: any; }) {
    const { globalData: { access, currentUser } } = getApp();
    this.setData({
      access,
      currentUser,
      topicId: options.topicId,
      mode: options.mode,
    })
    if (options.recordId !== 0) {
      this.setData({
        recordId: options.recordId
      })
    }
    this.loadCuesTopicDetails(this.data.topicId)
    this.getCuesSubmitPage({ current: this.data.current, pageSize: this.data.pageSize, topicId: this.data.topicId, mode: options.mode })
  },
  //拨打电话
  makePhoneCall(e: any) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone //仅为示例，并非真实的电话号码
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.loadCuesTopicDetails(this.data.topicId)
    const page = wx.getStorageSync('submit-page');// 存到缓存
    if(page==='create'){
      this.getCuesSubmitPage({ current: this.data.current, pageSize: this.data.pageSize, topicId: this.data.topicId,mode:this.data.mode })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    wx.setStorageSync('topicId', this.data.topicId)
    wx.setStorageSync('recordCount', this.data.recordCount)
    wx.setStorageSync('submitOrganizationCount', this.data.submitOrganizationCount)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    //导航条加载动画
    wx.showNavigationBarLoading()
    //loading 提示框
    wx.showLoading({
      title: '加载中...',
    })
    setTimeout(() => {
      wx.hideLoading();
      wx.hideNavigationBarLoading();
      this.getCuesSubmitPage({ current: this.data.current, pageSize: 1000, topicId: this.data.topicId })
      wx.stopPullDownRefresh();
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})